package br.com.efacta.bagarita.controllers;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.efacta.bagarita.domain.Discipline;
import br.com.efacta.bagarita.domain.Subject;
import br.com.efacta.bagarita.domain.repos.DisciplineRepository;
import br.com.efacta.bagarita.domain.repos.SubjectRepository;

@Controller
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectRepository repository;

    @Autowired
    private DisciplineRepository disciplineRepo;

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addSubject() {
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("subject", new Subject());
        context.put("disciplines", getAllDisciplines());
        context.put("persisted", Boolean.FALSE);
        return new ModelAndView("subject-edit", context);
    }

    @RequestMapping(value = "/edit/{subjectId}", method = RequestMethod.GET)
    public ModelAndView editSubject(@PathVariable("subjectId") String subjectId) {
        if (StringUtils.isNotBlank(subjectId)) {
            Subject subject = repository.findOne(subjectId);
            Map<String, Object> context = new HashMap<String, Object>();
            context.put("subject", subject);
            context.put("disciplines", getAllDisciplines());
            context.put("persisted", Boolean.FALSE);
            return new ModelAndView("subject-edit", context);
        }
        return null;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveSubject(@ModelAttribute("subject") Subject subject) {
        if (StringUtils.isBlank(subject.getId())) {
            subject.setId(null);
        }

        subject = repository.save(subject);
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("subject", subject);
        context.put("disciplines", getAllDisciplines());
        context.put("persisted", Boolean.TRUE);

        return new ModelAndView("subject-edit", context);
    }

    @RequestMapping(value = "delete/{subjectId}", method = RequestMethod.GET)
    public ModelAndView deleteSubject(@PathVariable("subjectId") String subjectId) {
        if (StringUtils.isNotBlank(subjectId)) {
            repository.delete(subjectId);
            return new ModelAndView("redirect:subject-list");
        }
        return null;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listSubjects() {
        Iterable<Subject> subjects = repository.findAll();
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("subjects", subjects);
        return new ModelAndView("subject-list", context);
    }

    private Iterable<Discipline> getAllDisciplines() {
        return disciplineRepo.findAll();
    }
}
